package com.naiterui.ehp.bs.emr.controller.patient;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IPatientHealthRecordService;
import com.naiterui.ehp.bs.emr.vo.health.HealthRecordVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description 患者健康档案
 * @Author gongbaoqiang
 * @Date 2020/6/2 5:18 PM
 */
@Slf4j
@RestController
@RequestMapping("emr/patient/health/record")
@Api(tags = SwaggerTag.PATIENT + SwaggerTag.MEDICAL_RECORD)
public class EmrPatientHealthRecordController {


    @Autowired
    private IPatientHealthRecordService healthRecordService;
    /**
     * 档案存草稿，草稿状态医生不能预览
     * @param familyId
     * @param recordVO
     * @return
     * @throws BusinessException
     */
    @PostMapping("cache/{familyId}")
    @ApiOperation(value = "存草稿", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "familyId", value = "家庭成员关系id", required = true, dataType = "long", paramType = "path"),
    })
    public ResponseVO<Void> cache(@PathVariable @NotNull Long familyId, @RequestBody HealthRecordVO recordVO) throws BusinessException {
        log.info("健康档案保存草稿：familyId={}, recordVO={}", familyId, recordVO.toString());
        healthRecordService.cacheRecord(familyId, recordVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 档案提交，提交后医生才可以预览
     * @param familyId
     * @param recordVO
     * @return
     * @throws BusinessException
     */
    @PostMapping("submit/{familyId}")
    @ApiOperation(value = "档案提交", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "familyId", value = "家庭成员关系id", required = true, dataType = "long", paramType = "path"),
    })
    public ResponseVO<Void> submit(@PathVariable @NotNull Long familyId,@RequestBody HealthRecordVO recordVO) throws BusinessException {
        log.info("健康档案提交：familyId={}, recordVO={}", familyId, recordVO);

        //必填校验
        recordVO.validate();

        healthRecordService.submit(familyId, recordVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 档案详情
     * @param familyId
     * @return
     * @throws BusinessException
     */
    @PostMapping("detail/{familyId}")
    @ApiOperation(value = "档案详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "familyId", value = "家庭成员关系id", required = true, dataType = "long", paramType = "path"),
    })
    public ResponseVO<HealthRecordVO> detail(@PathVariable @NotNull Long familyId) throws BusinessException {
        log.info("健康档案详情：familyId={}", familyId);
        return PublicService.returnResponseVO(healthRecordService.detail(familyId));
    }


}
